package com.match.competition_sub.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.match.competition_sub.entity.CompetitionDepartment;
import com.match.competition_sub.entity.Student;
import com.match.competition_sub.entity.vo.StuQuery;
import com.match.competition_sub.excel.StudentData;
import com.match.competition_sub.listener.StudentExcelListener;
import com.match.competition_sub.mapper.StudentMapper;
import com.match.competition_sub.service.CompetitionDepartmentService;
import com.match.competition_sub.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AdiosF
 * @since 2021-09-21
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private CompetitionDepartmentService competitionDepartmentService;


    /**
     * 根据学生学号更新学生状态
     * @param stuNum
     * @param state
     * @return
     */
    @Override
    public Boolean updateStuState(String stuNum, int state) {
        Boolean b=baseMapper.updateByStuNumber(stuNum,state);
        return b;
    }


    /**
     *分页条件查询学生信息
     * @param current
     * @param limit
     * @param stuQuery
     * @return
     */
    @Override
    public Map<String,Object> findStuByQuery(long current, long limit, StuQuery stuQuery) {
        String lever; //系别
        String majorNam;   //专业名称

        // 多条件组合查询
        String stuNumber = stuQuery.getStuNumber();
        lever = stuQuery.getLever();
        if (!StringUtils.isEmpty(lever)){
            CompetitionDepartment department = competitionDepartmentService.getById(lever);
            //系别名称
            String department1 = department.getDepartment();
            lever=department1;
        }
        majorNam = stuQuery.getMajorNum();
        if (!StringUtils.isEmpty(majorNam)){
            CompetitionDepartment department = competitionDepartmentService.getById(majorNam);
            //专业名称
            String department2 = department.getDepartment();
            majorNam=department2;
        }
        String begin = stuQuery.getBegin();
        String end = stuQuery.getEnd();

        Page<Student> page=new Page<>(current,limit);
        //构建条件
        QueryWrapper<Student> studentQueryWrapper=new QueryWrapper<>();
        //非空条件判断
        if (!StringUtils.isEmpty(stuNumber)){
            studentQueryWrapper.like("number",stuNumber);
        }
        if (!StringUtils.isEmpty(lever)){
            studentQueryWrapper.eq("lever",lever);
        }
        if (!StringUtils.isEmpty(majorNam)){
            studentQueryWrapper.eq("major_nam",majorNam);
        }
        if(!StringUtils.isEmpty(begin)) {
            studentQueryWrapper.ge("gmt_modified",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            studentQueryWrapper.le("gmt_modified",end);
        }
        //按时间升序排序
        studentQueryWrapper.orderByDesc("gmt_modified");
        //排除密码
        studentQueryWrapper.select(Student.class,i ->!i.getColumn().equals("password"));
        baseMapper.selectPage(page, studentQueryWrapper);
        Map<String,Object> map=new HashMap<>();
        long total = page.getTotal();//总记录数
        List<Student> records = page.getRecords(); //数据list集合
        map.put("total",total);
        map.put("records",records);
        return map;
    }

    /**
     * EasyExcel录入学生信息
     * @param file
     * @param studentService
     */
    @Override
    public void addStuByExcel(MultipartFile file, StudentService studentService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, StudentData.class,new StudentExcelListener(studentService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 更新报名状态
     * @param groutId
     * @param replace
     */
    @Override
    public void updateSignUpState(String groutId, int replace, int state) {
        baseMapper.updateSignUpState(groutId,replace,state);
    }

}
